from flask import Flask,jsonify,request
#导入配制文件
from config import Config
from db import db1
app = Flask(__name__)
#加载配制文件
app.config.from_object(Config)
#跨域配制
from flask_cors import CORS
CORS(app)
import json
#测试添加展示
@app.route('/test',methods=['POST',"GET"])
def test():
    if request.method == "POST":
        list = json.loads(request.data)
        print(list)
        sql1 = "insert into test values(0,'%s')"%(list['list'])
        print(sql1)
        db1.update(sql1)
       
    else:
        sql = "select * from test where id=1"
        res = db1.find(sql)
        return jsonify({"res":res})

def get_data():
    data = request.data
    if data:
        data = json.loads(data)
    else:
        data = request.form
    return data
    
#添加展示工作流接口
@app.route('/workfM',methods=['POST','GET'])
def workfM():
    if request.method == 'POST':
        #添加
        #获取用户输入
        data = get_data()
        sql = "insert into worktemp(name) values('%s')"%(data['name'])
        db1.update(sql)
        return jsonify({"code":200})
    else:
        #展示
        sql = "select * from worktemp"
        res = db1.find_all(sql)
        return jsonify({"code":200,'list':res})

#设置工作流属性
@app.route('/setparams',methods=['POST','GET'])
def setparams():
    if request.method == 'POST':
        data = get_data()
        #更新工作流中的参数
        sql= "update worktemp set params='%s' where id=%d"%(data['params'],int(data['id'])) 
        db1.update(sql)
        return jsonify({"code":200})
    else:
        id = request.args.get('wid')
        sql = "select * from worktemp where id=%d"%(int(id))
        res = db1.find(sql)
        return jsonify({'code':200,'workf':res})

if __name__ == "__main__":
    app.run(debug=True)